<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/model/Model_Mysql.php');

class Model_UserRelations extends Model_Mysql {
	const MYSELF = 0;
	const PENDING_COUPLE = 1;
	const PENDING_FRIEND = 2;
	const COUPLE = 4;
	const FRIEND = 8;
	const NO_RELATION = 16;
	
	//inserting same row 2 times (1,2) and (2,1) to make db distributed
	public function getStatus($my_uid, $friend_uid) {
		$query = DB::query(Database::SELECT, "SELECT sender_uid,status FROM users_relation
				WHERE uid = $my_uid AND friend_uid = $friend_uid");
		$res = $this->execute_single($query);
		if($res == false) return array('status' =>self::NO_RELATION);
		return $res;
	}
	public function getPartner($uid) {
		$query = DB::query(Database::SELECT, "SELECT friend_uid FROM users_relation
				WHERE uid = $uid AND status = ".self::COUPLE);
		$res = $this->execute_single($query);
		if($res == false) return null;
		return $res['friend_uid'];
	}
	public function getFriends($uid) {
		$query = DB::query(Database::SELECT, "SELECT users_relation.friend_uid AS uid, 
			users_profile.f_name AS name,users_profile.pic_url AS pic_url FROM users_relation, users_profile WHERE 
			users_relation.uid = $uid AND users_relation.friend_uid = users_profile.uid AND 
			users_relation.status = ".self::FRIEND);
		$res = $this->execute($query);
		if($res == false) return null;
		return $res;
	}
	public function sendFriendRequest($sender, $receiver) {
		$now = time();
		$query = DB::query(Database::INSERT, "INSERT INTO users_relation(uid,friend_uid,sender_uid,
				status,timestamp)VALUES(:uid,:friend,$sender,".self::PENDING_FRIEND.", $now)
				ON DUPLICATE KEY UPDATE status=(status & ~".self::FRIEND.") | ".self::PENDING_FRIEND);
		$query->param(":uid", $sender);
		$query->param(":friend", $receiver);
		$this->execute($query);
		$query->param(":uid", $receiver);
		$query->param(":friend", $sender);
		return $this->execute($query);
	}
	public function cancelFriendRequest($sender, $receiver) {
		$now = time();
		$query = DB::query(Database::UPDATE, "UPDATE users_relation SET timestamp=$now,
				status=(status & ~".self::PENDING_FRIEND.") | ".self::FRIEND.
				" WHERE status = ".self::PENDING_FRIEND." AND sender_uid=$sender AND
				((uid=$sender AND friend_uid=$receiver)OR(uid=$receiver AND friend_uid=$sender))");
		return $this->execute($query);
	}
	public function acceptFriendRequest($sender, $receiver) {
		$now = time();
		$query = DB::query(Database::UPDATE, "UPDATE users_relation SET timestamp=$now,
				status=(status & ~".self::PENDING_FRIEND.") | ".self::FRIEND.
				" WHERE status = ".self::PENDING_FRIEND." AND sender_uid=$sender AND
				((uid=$sender AND friend_uid=$receiver)OR (uid=$receiver AND friend_uid=$sender))");
		return $this->execute($query);
	}
	public function sendCoupleRequest($sender, $receiver) {
		$now = time();
		$query = DB::query(Database::INSERT, "INSERT INTO users_relation(uid,friend_uid,sender_uid,
				status,timestamp)VALUES(:uid,:friend,$sender,".self::PENDING_COUPLE.", $now)
				ON DUPLICATE KEY UPDATE status=(status & ~".self::COUPLE.") | ".self::PENDING_COUPLE);
		$query->param(":uid", $sender);
		$query->param(":friend", $receiver);
		$this->execute($query);
		$query->param(":uid", $receiver);
		$query->param(":friend", $sender);
		return $this->execute($query);
	}
	public function cancelCoupleRequest($sender, $receiver) {
		$now = time();
		$query = DB::query(Database::UPDATE, "UPDATE users_relation SET timestamp=$now,
				status=(status & ~".self::PENDING_COUPLE.") | ".self::COUPLE.
				" WHERE sender_uid=$sender AND status = ".self::PENDING_COUPLE." AND
				((uid=$sender AND friend_uid=$receiver)OR(uid=$receiver AND friend_uid=$sender))");
		return $this->execute($query);
	}
	public function acceptCoupleRequest($sender, $receiver) {
		$now = time();
		$query = DB::query(Database::UPDATE, "UPDATE users_relation SET timestamp=$now,
				status=(status & ~".self::PENDING_COUPLE.") | ".self::COUPLE.
				" WHERE sender_uid=$sender AND status = ".self::PENDING_COUPLE." AND
				((uid=$sender AND friend_uid=$receiver)OR(uid=$receiver AND friend_uid=$sender))");
		return $this->execute($query);
	}
}